package com.photoeditor.demo.model.preference;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;

import com.common.base.ComponentContext;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 子进程接收主进程特定消息广播接收器
 * Created by edward on 17-11-7.
 */

public class ChildProcessBroadcastReceiver extends BroadcastReceiver{
    public static final String ACTION = "com.photoeditor.demo.model.preference.ChildProcessBroadcastReceiver";

    public static final String TYPE = "TYPE";
    public static final String STATUS = "STATUS";
    public static final String DATA = "DATA";

    public static final int TYPE_REFRESH_PRELOAD_DATA = 1; // 刷新预加载数据
    public static final int TYPE_REFRESH_HOMEPAGE_DATA = 2; // 刷新主页数据
    public static final int TYPE_REFRESH_STORE_DATA = 3; // 刷新商店页数据
    public static final int TYPE_PRELOAD_FILE_COMPLETE = 4; // 预加载文件下载完成


    private static ChildProcessBroadcastReceiver sInstance;
    private List<OnMainProcessMsgListener> mMsgListenerList;
    private Context mContext;

    private ChildProcessBroadcastReceiver() {
        mMsgListenerList = new ArrayList<>();
        mContext = ComponentContext.getContext();
    }

    public synchronized static ChildProcessBroadcastReceiver getInstance() {
        if (sInstance == null) {
            sInstance = new ChildProcessBroadcastReceiver();
        }
        return sInstance;
    }

    public void init() {
        IntentFilter intentFilter = new IntentFilter(ACTION);
        mContext.registerReceiver(this, intentFilter);
    }

    public void destroy() {
        mMsgListenerList.clear();
        try {
            mContext.unregisterReceiver(this);
        } catch (Exception ignore) {}
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }
        String action = intent.getAction();
        if (!TextUtils.equals(action, ACTION)) {
            return;
        }
        int type = intent.getIntExtra(TYPE, -1);
        int status = intent.getIntExtra(STATUS, -1);
        Serializable data = intent.getSerializableExtra(DATA);
        if (mMsgListenerList.size() > 0) {
            for (OnMainProcessMsgListener listener : mMsgListenerList) {
                listener.onMainProcessMsgReceive(type, status, data);
            }
        }
    }

    public synchronized void registerListener(OnMainProcessMsgListener listener) {
        mMsgListenerList.add(listener);
    }

    public synchronized void unregisterListener(OnMainProcessMsgListener listener) {
        mMsgListenerList.remove(listener);
    }

    public interface OnMainProcessMsgListener {
        /**
         * 收到主进程消息
         * @param type 消息类型
         * @param status 特定状态，依据特定业务处理，默认为-1
         * @param data 特定数据，依据特定业务处理，默认为null
         */
        void onMainProcessMsgReceive(int type, int status, Serializable data);
    }


    /**
     * 发送广播消息
     * @param type
     */
    public void sendBroadcastMsg(int type) {
        Intent intent = new Intent(ACTION);
        intent.putExtra(TYPE, type);
        mContext.sendBroadcast(intent);
    }

    public void sendBroadcastMsg(int type, int status) {
        Intent intent = new Intent(ACTION);
        intent.putExtra(TYPE, type);
        intent.putExtra(STATUS, status);
        mContext.sendBroadcast(intent);
    }

    public void sendBroadcastMsg(int type, Serializable data) {
        Intent intent = new Intent(ACTION);
        intent.putExtra(TYPE, type);
        intent.putExtra(DATA, data);
        mContext.sendBroadcast(intent);
    }

    public void sendBroadcastMsg(int type, int status, Serializable data) {
        Intent intent = new Intent(ACTION);
        intent.putExtra(TYPE, type);
        intent.putExtra(STATUS, status);
        intent.putExtra(DATA, data);
        mContext.sendBroadcast(intent);
    }
}
